package com.ltw.modules.test.dao;

import com.ltw.modules.common.entity.SearchBean;
import com.ltw.modules.test.entity.City;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
@Mapper
public interface CityDao {
    @Insert("insert into test_city (city_name, local_city_name, country_id, date_created) " +
            "values(#{cityName}, #{localCityName}, #{countryId}, #{dateCreated})")
    @Options(useGeneratedKeys = true, keyColumn = "city_id", keyProperty = "cityId")
    void insertCity(City city);

    @Update("update test_city set city_name = #{cityName}," +
            "local_city_name= #{localCityName} where city_id= #{cityId}")
    void updateCity(City city);
    @Delete("delete from test_city where city_id= #{cityId} ")
    void deleteCityByCityId(int cityId);

    @Select("select * from test_city where city_id= #{cityId} ")
    City queryCityByCityId(int cityId);



    @Select("select * from test_city where city_name=#{cityName}" +
            "and local_city_name=#{localCityName}")
    City queryCityByCityNameAndLocalCityName(@Param("cityName") String cityName,
                                             @Param("localCityName") String localCityName);


    @Select("select * from test_city where country_id = #{countryId}")
    List<City> getCitiesByCountryId(int countryId);

    @Select("<script>"
            + "select * from test_city "
            + "<where> "
            + "<if test='keyword != \"\" and keyword != null'>"
            + " and (city_name like '%${keyword}%' "
            + "or local_city_name like '%${keyword}%') "
            + "</if>"
            + "</where>"
            + "<choose>"
            + "<when test='orderBy != \"\" and orderBy != null'>"
            + " order by ${orderBy} ${direction}"
            + "</when>"
            + "<otherwise>"
            + " order by city_id ${direction}"
            + "</otherwise>"
            + "</choose>"
            + "</script>")
    List<City> getCitiesBySearchBean(SearchBean searchBean);

}
